package cn.tedu.net.collection.file;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.*;

//本类用于测试字符流的读取
public class Test5_In2<main> {
    public static void main(String[] args) {
        method1();//普通字符流的读取
        method2();//高效字符流的读取
    }
//测试普通字符流读取
    public static void method1() {
        //创建普通字符流对象
//        Reader in=new Reader(); 字符读取流的抽象父类
        Reader in=null;
        Reader in2=null;
        try {
            in=new FileReader(new File("D:\\ready\\4.txt"));
            in2=new FileReader("D:\\ready\\4.txt");
            //开始读取
//            System.out.println(in.read());每次读取一个字符,如果读取到了数据末尾返回-1
            int b;
            while ((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //释放资源
            try {
                in.close();
                in2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
//测试高效字符流读取
    public static void method2() {
        Reader in=null;
        try {
            /*BufferedReader是一个高效字符读取流
            * 底层维护了一个char[].数组的长度是8*1024=8192字节8kb*/
            in=new BufferedReader(new FileReader("D:\\ready\\4.txt"));
            int b;
            while ((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
